local skel = fk.CreateSkill {
  name = "emo__xianyou",
  tags = {Skill.Permanent},
}

skel:addEffect(fk.TurnedOver, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player.faceup then
      room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      if player.dead then return end
      if player:usedSkillTimes("emo__maoxian", Player.HistoryGame) == 0 then
        local skills = table.filter(player:getTableMark("emo__xianyou_skills"), function (s) return player:hasSkill(s, true) end)
        if #skills > 1 then
          local to_keep = room:askToChoice(player, {
            choices = skills, detailed = true, cancelable = false, skill_name = skel.name,
            prompt = "#emo__xianyou-delete",
          })
          table.removeOne(skills, to_keep)
          room:handleAddLoseSkills(player, "-"..table.concat(skills, "|-"))
        end
      end
    else
      player:drawCards(3, skel.name)
      if player.dead then return end
      local exist = {}
      for _, p in ipairs(room.players) do
        table.insertIfNeed(exist, p.general)
        table.insertIfNeed(exist, p.deputyGeneral)
      end
      local all_generals = table.filter(room.general_pile, function (g) return not table.contains(exist, g) end)
      if #all_generals == 0 then return end
      local generals, skillList = {}, {}
      while #all_generals > 0 and #generals < 3 do
        local g = table.remove(all_generals, math.random(1, #all_generals))
        local general = Fk.generals[g]
        local skills = {}
        local tags = {Skill.Lord, Skill.Quest, Skill.Wake, Skill.Switch, Skill.AttachedKingdom}
        for _, skillName in ipairs(general:getSkillNameList()) do
          local s = Fk.skills[skillName]
          if not player:hasSkill(s) and table.every(tags, function (tag) return not s:hasTag(tag) end) then
            table.insert(skills, skillName)
          end
        end
        if #skills > 0 then
          table.insert(generals, g)
          table.insert(skillList, skills)
        end
      end
      if #generals == 0 then return end
      local result = room:askToCustomDialog( player, {
        skill_name = skel.name,
        qml_path = "packages/utility/qml/ChooseSkillFromGeneralBox.qml",
        extra_data = { generals, skillList, "#emo__xianyou-gainskill" },
      })
      if result == "" then
        result = {generals[1], skillList[1][1]}
      end
      local generalName, skill = table.unpack(result)
      room:addTableMark(player, "emo__xianyou_skills", skill)
      room:handleAddLoseSkills(player, skill)
    end
  end,
})


Fk:loadTranslationTable{
  ["emo__xianyou"] = "仙游",
  [":emo__xianyou"] = "持恒技，当你翻至背面时，你摸三张牌，并从随机3个武将中选择一个技能获得。当你翻至正面时，回复1点体力，保留一个以此法获得的技能并移除其余。",
  ["#emo__xianyou-gainskill"] = "仙游：获得一个技能",
  ["#emo__xianyou-delete"] = "仙游：选择1个技能保留，其余的删除",
}

return skel
